package GAM
{
	import flash.display.Sprite;

	public class snake extends Sprite
	{
		public var Snakehead:snakehead;
		private var Snakebody:snakebody;
		public var directarr:Array=["N","E","W","S"];
		private var array:Array=new Array();
		public var direction:String;
		public function snake()
		{
			init();
		}
		public function init():void
		{
			for(var i:int=0;i<2;i++)
			{
				Snakebody=new snakebody();
				Snakebody.x=i*25;
				Snakebody.y=100;
				addChild(Snakebody);
				array.push(Snakebody);
			}
			Snakehead=new snakehead(3,5);
			Snakehead.x=50;
			Snakehead.y=100;
			addChild(Snakehead);
			
			direction="S"
		}
		public function move():void
		{
		
			
			switch(direction)
			{
				case "N" :
					movenext(0,-1);
					break;
				case "E" :
					movenext(1,0);
					break;
				case "W" :
					movenext(-1,0);
					break;
				case "S" :
					movenext(0,1)
					break;
				default:
					break;
			}
		}
		public function movenext(x:int,y:int):void
		{
			
			var oldlocationx:int=Snakehead.x;
			var oldlocationy:int=Snakehead.y;
			Snakehead.x+=25*x;
			Snakehead.y+=25*y;

			
			for(var i:int=0;i<array.length-1;i++)
			{
				array[i].x=array[i+1].x;
				array[i].y=array[i+1].y;
			}
			array[i].x=oldlocationx;
			array[i].y=oldlocationy;
			
			
		}
		public function addChildNode():void
		{
			var oldlocationx:int=array[array.length-1].x;
			var oldlocationy:int=array[array.length-1].y;
			
			switch(direction)
			{
				case "N":
					movenode(0,1);
					break;
				case "E":
					movenode(-1,0);
					break;
				case "W":
					movenode(1,0);
					break;
				case "S":
					movenode(0,-1);
					break;
			}
			Snakebody=new snakebody();
			Snakebody.x=oldlocationx;
			Snakebody.y=oldlocationy;
			array.push(Snakebody);
			addChild(Snakebody);
			
			trace(array[0].x);
			
			
			
		}
		public function movenode(x:int,y:int):void
		{
			trace("array[0].ex"+array[0].x);
			trace("array[1].ex"+array[1].x);
			var movex:int=array[0].x+x*25;
			var movey:int=array[0].y+y*25;
			trace("array[0].ex"+array[0].x);
			trace("array[1].ex"+array[1].x);
			for(var i:int=array.length-1;i>0;i--)
			{
				array[i].x=array[i-1].x;
				array[i].y=array[i-1].y;
			}
			array[0].x=movex;
			array[0].y=movey;
		}
		public function repeat(x:int,y:int):Boolean
		{
			for(var i:int=0;i<array.length;i++)
			{
				if(array[i].x==x&&array[i].y==y)
				{
					return true;
				}
			}
			
			return false;
		}
		public function complete():Boolean
		{
			if(array.length>11)
				return true;
			return false;
		}
		
	}
}